package com.shiro.demo.web;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shiro.demo.entity.User;
import com.shiro.demo.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/gotoUserList")
	@RequiresPermissions("user:view")//权限管理;
	public String gotoUserList(Model model) {
		List<User> users = userService.getAll();
		model.addAttribute("users", users);
		return "user/list";
	}

	@RequestMapping("/getUser")
	public User getUser(Long id) {
		User user = userService.findUserById(id);
		return user;
	}

	@RequestMapping("/add")
	public String insert(User user) {
		userService.insert(user);
		return "redirect:/gotoUserList";
	}
	
	@RequestMapping("/gotoUserAdd")
	@RequiresPermissions("user:add")
    public String toAdd() {
        return "user/userAdd";
    }

	@RequestMapping("/gotoUserEdit")
	public String toEdit(Model model, Long id) {
		User user = userService.findUserById(id);
		model.addAttribute("user", user);
		return "user/userEdit";
	}

	@RequestMapping(value = "update")
	@RequiresPermissions("user:update")
	public String update(User user) {
		userService.update(user);
		return "redirect:/gotoUserList";
	}

	@RequestMapping(value = "/delete")
	@RequiresPermissions("user:del")
	public String delete(Long id) {
		userService.delete(id);
		return "redirect:/gotoUserList";
	}

}
